﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cjwygl
{
    public partial class FrmMainfeeReport : Form
    {
        public List<ComboItem> collection = new List<ComboItem>();

        public FrmMainfeeReport()
        {
            InitializeComponent();
            InitializeComboSearch();

            comboBox_customer.SelectedIndex = 0;
            comboBox_genMonth.SelectedIndex = 0;
            comboBox_paid.SelectedIndex = 0;
        }

        public void InitializeComboSearch()
        {
            collection.Add(new ComboItem { Text = "所有列", Value = "ALL" });
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                collection.Add(new ComboItem
                {
                    Text = dataGridView1.Columns[i].HeaderText,
                    Value = dataGridView1.Columns[i].DataPropertyName
                });
            }
            comboBox_cols.DisplayMember = "Text";
            comboBox_cols.ValueMember = "Value";
            comboBox_cols.DataSource = collection;
            comboBox_cols.SelectedValue = "ALL";


            comboBox_customer.Items.Add("<全部>");
            using (DataBase db = new DataBase())
            {
                string sql = "SELECT COMPANY_NAME FROM CJ_CUSTOMER_INFO order by COMPANY_NAME";
                DataTable dt = db.ExecuteDataTable(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    comboBox_customer.Items.Add(dr[0].ToString());
                }
            }

            comboBox_genMonth.Items.Add("<全部>");
            using (DataBase db = new DataBase())
            {
                string sql = "SELECT DISTINCT GEN_MONTH FROM CJ_MAIN_FEE order by GEN_MONTH";
                DataTable dt = db.ExecuteDataTable(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    comboBox_genMonth.Items.Add(dr[0].ToString());
                }
            }

        }

        private void LoadData()
        {
            if (comboBox_customer.SelectedIndex != 0 || comboBox_genMonth.SelectedIndex != 0 ||
                comboBox_paid.SelectedIndex != 0)
            {

                using (DataBase db = new DataBase())
                {
                    String sql = "SELECT * FROM View_MAIN_FEE_REPORT WHERE 1=1 ";
                    if (comboBox_customer.SelectedIndex != 0)
                    {
                        sql += " AND COMPANY_NAME=@COMPANY_NAME ";
                        db.AddParameter("COMPANY_NAME", comboBox_customer.Text);
                    }
                    if (comboBox_genMonth.SelectedIndex != 0)
                    {
                        sql += " AND GEN_MONTH=@GEN_MONTH ";
                        db.AddParameter("GEN_MONTH", comboBox_genMonth.Text);
                    }
                    if (comboBox_paid.SelectedIndex == 1)
                    {
                        sql += " AND IS_PAID=1 ";
                    }
                    else if (comboBox_paid.SelectedIndex == 2)
                    {
                        sql += " AND IS_PAID=0 ";
                    }

                    DataTable dt = db.ExecuteDataTable(sql);
                    LoadData(dt);
                }
            }
            else
            {
                this.viewMAINFEEREPORTBindingSource.DataSource = this.cjwyglDataSet_mainfee_report;
                this.viewMAINFEEREPORTBindingSource.DataMember = "View_MAIN_FEE_REPORT";
                
                this.view_MAIN_FEE_REPORTTableAdapter.Fill(this.cjwyglDataSet_mainfee_report.View_MAIN_FEE_REPORT);
            }
        }

        private void LoadData(DataTable dt)
        {
            this.viewMAINFEEREPORTBindingSource.DataSource = dt;
            this.view_MAIN_FEE_REPORTTableAdapter.Fill(this.cjwyglDataSet_mainfee_report.View_MAIN_FEE_REPORT);
        }

        private void FrmMainfeeReport_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void button_commonsearch_Click(object sender, EventArgs e)
        {
            string sql = "";
            string key = textBox_search.Text.Trim().Replace("'", "''");
            if (key == "")
            {
                MessageBox.Show("请输入查询关键词");
                return;
            }
            if (comboBox_cols.SelectedValue.ToString() == "ALL")
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("SELECT * FROM View_MAIN_FEE_REPORT WHERE 1=2 ");
                for (int i = 1; i < collection.Count; i++)
                {
                    sb.Append("or " + collection[i].Value + " like '%" + key + "%' ");
                }
                sql = sb.ToString();
            }
            else
            {
                sql = "SELECT * FROM View_MAIN_FEE_REPORT WHERE " +
                    comboBox_cols.SelectedValue + " like '%" + key + "%' ";
            }
            using (DataBase db = new DataBase())
            {
                DataTable dt = db.ExecuteDataTable(sql);
                LoadData(dt);
            }
        }

        private void button_refresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void button_modify_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }


            FrmSinglefeeMgr frmFeeMgr = new FrmSinglefeeMgr(dataGridView1.SelectedRows[0].Cells[2].Value.ToString(),
                dataGridView1.SelectedRows[0].Cells[4].Value.ToString());
            frmFeeMgr.ShowDialog();
        }

        private void button_delete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }

            DialogResult dr = MessageBox.Show("确定要删除吗？", "删除该条记录", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.Cancel)
            {
                return;
            }


            // 删除客户在此费用年月上的所有单项费用
            using (DataBase db = new DataBase())
            {
                db.AddParameter("COMPANY_ID", dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                db.AddParameter("GEN_MONTH", dataGridView1.SelectedRows[0].Cells[4].Value.ToString());
                int ret = db.ExecuteNonQuery("DELETE FROM CJ_MAIN_FEE WHERE COMPANY_ID = @COMPANY_ID AND GEN_MONTH = @GEN_MONTH");
                if (ret > 0)
                {
                    MessageBox.Show("已删除该记录及其" + ret + "个单项费用记录");
                }
                else
                {
                    MessageBox.Show("删除操作失败");
                }
            }
        }

        private void button_paid_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                MessageBox.Show("未选中任何行");
                return;
            }


            bool setTo = !(bool)dataGridView1.SelectedRows[0].Cells[5].Value;
            if (setTo)
            {
                DialogResult dr = MessageBox.Show("确定要将改条记录标记为已付款吗？", "标记为已付款", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }
            }
            else
            {
                DialogResult dr = MessageBox.Show("选定的记录已经标记为已付款，是否将其标记为未付款？", "标记为未付款", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (dr == DialogResult.No)
                {
                    return;
                }
            }



            using (DataBase db = new DataBase())
            {
                db.AddParameter("COMPANY_ID", dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                db.AddParameter("GEN_MONTH", dataGridView1.SelectedRows[0].Cells[4].Value.ToString());
                db.AddParameter("IS_PAID", setTo ? 1 : 0);
                db.AddParameter("MODIFY_COMMENT", DateTime.Now.ToString() + ":" + userinfo.username + (setTo ? "标记为已付款;" : "标记为未付款;"));
                int ret = db.ExecuteNonQuery("UPDATE CJ_MAIN_FEE SET IS_PAID=@IS_PAID,MODIFY_COMMENT=MODIFY_COMMENT+@MODIFY_COMMENT WHERE COMPANY_ID = @COMPANY_ID AND GEN_MONTH = @GEN_MONTH");
                if (ret > 0)
                {
                    MessageBox.Show("操作成功");
                }
                else
                {
                    MessageBox.Show("操作失败");
                }
            }
        }

        private void button_print_Click(object sender, EventArgs e)
        {
            FrmMonthSel frmMS = new FrmMonthSel();
            frmMS.ShowDialog();
        }

    }
}
